package com.example.springboot.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.springboot.entity.Collect;
import com.example.springboot.entity.dto.CollectDto;
import com.example.springboot.mapper.CollectMapper;
import com.example.springboot.mapper.PostMapper;
import com.example.springboot.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private PostMapper postMapper;
    @Override
    @Transactional
    public void collect(CollectDto collectDto, Integer collectId) {
        Collect collect = BeanUtil.copyProperties(collectDto,Collect.class);
        collect.setCollectId(collectId);
        if(collectMapper.isEmpty(collect) == null){
            collectMapper.insertCollect(collect);
            postMapper.increCollectCount(collectDto.getCollectedId());
        }else {
            collectMapper.updateCollect(collect);
            if(collect.getIsCollect() == 1){
                postMapper.increCollectCount(collectDto.getCollectedId());
            }else{
                postMapper.decreCollectCount(collectDto.getCollectedId());
            }
        }

    }

    @Override
    public Collect getIsCollect(Integer collectedId, Integer collectId) {
        Collect collect = new Collect();
        collect.setCollectedId(collectedId);
        collect.setCollectId(collectId);
        return collectMapper.isEmpty(collect);
    }
}
